#include "StdAfx.h"
#include ".\apssimage.h"
#include "apFilelog.h"

apSSImage::apSSImage(void)
:m_hImage(NULL)
{
}

apSSImage::~apSSImage(void)
{
}

bool apSSImage::Init()
{
	return true;
}

void apSSImage::Close()
{
}

bool apSSImage::CreateImage_file(const char* filename)
{
	NResult result;

	if(m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is not released");
		return false;
	}

	result = NImageCreateFromFileEx((NChar *)filename, NULL, 0, NULL, &m_hImage);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to create image from file(result = %d)", result);
		return false;
	}

	return true;
}

bool apSSImage::CreateImage_data(const char* pData, int width, int height, 
							int bpp, int pixelFMT)
{
	NResult result;
	NPixelFormat pf;

	if(m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is not released");
		return false;
	}

	if(bpp == 24)
		pf = NPF_RGB_8U;//RGBRGB....
	else
		pf = NPF_RGB_A_8U;//RGBARGBA...
	result = NImageCreateFromDataEx(pf, width, height, 0,
		width * (bpp / 8), (void *)pData, width * height * (bpp / 8),
		0, &m_hImage);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to create image from data(result = %d)", result);
		return false;
	}

	return true;
}

bool apSSImage::CreateImage_data_part(const char* pData, int width, int height, 
						   int bpp, int left, int top, int desWidth, int desHeight,
						   int pixelFMT)//format: 0-RGB24, 1-RGB32
{
	NResult result;
	NPixelFormat pf;

	if(m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is not released");
		return false;
	}

	if(bpp == 24)
		pf = NPF_RGB_8U;//RGBRGB....
	else
		pf = NPF_RGB_A_8U;//RGBARGBA...

	result = NImageCreateFromDataPartEx(pf, 0, width, height,
		width * (bpp / 8), (void *)pData, width * height * (bpp / 8),
		left, top, desWidth, desHeight,
		0, &m_hImage);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to create image from data part(result = %d)", result);
		return false;
	}

	return true;
}

bool apSSImage::CreateImage_image(HNImage hImage)
{
	NResult result;
	NPixelFormat pixelFormat;

	if(m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is not released, failed to create");
		return false;
	}

	result = NImageCreateFromImageEx2(pixelFormat, 0, hImage, 0, &m_hImage);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to create from image(result = %d)", result);
		return false;
	}
	return true;
}

bool apSSImage::CreateImage_image_part(HNImage hImage, int left, int top,
							int desWidth, int desHeight)
{
	NResult result;
	NPixelFormat pixelFormat;
	
	if(m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is not released, failed to create");
		return false;
	}

	pixelFormat = NPF_RGB_8U;
	result = NImageCreateFromImagePartEx2(pixelFormat, 0,
		hImage, left, top, desWidth, desHeight, 0, &m_hImage);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to create from image part(result = %d)", result);
		return false;
	}

	return true;
}

bool apSSImage::ConvertToGray()
{
	NResult result;
	HNGrayscaleImage hImage = NULL;

	if(!m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is null, failed to convert");
		return false;
	}

	result = NImageToGrayscale(m_hImage, &hImage);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to convert gray(result = %d)", result);
		return false;
	}

	NObjectFree(m_hImage);
	m_hImage = hImage;
	return true;
}

bool apSSImage::ConvertToMono()
{
	NResult result;
	HNMonochromeImage hImage = NULL;

	if(!m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is null, failed to convert");
		return false;
	}
	result = NImageToMonochrome(m_hImage, &hImage);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to convert mono(result = %d)", result);
		return false;
	}

	NObjectFree(m_hImage);
	m_hImage = hImage;
	return true;
}

bool apSSImage::FillBitmap(HBITMAP *phBMP)
{
	NResult result;

	if(!m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is null, failed to get bitmap");
		return false;
	}

	result = NImageToHBitmap(m_hImage, phBMP);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to get bitmap(result = %d)", result);
		return false;
	}

	return true;
}

void apSSImage::DumpImageInfo()
{
	NResult			result;
	NFloat			hRes, vRes;//resolution
	NUInt			width, height;
	NPixelFormat	pf;
	NSizeType		stride;
	NSizeType		image_size;

	if(!m_hImage)
	{
		apLog::print(0, apLog::warning, "internal image is null, no info dump");
		return;
	}

	result = NImageGetPixelFormat(m_hImage, &pf);
	result = NImageGetWidth(m_hImage, &width);
	result = NImageGetHeight(m_hImage, &height);
	result = NImageGetImageSize(m_hImage, &image_size);
	result = NImageGetVertResolution(m_hImage, &vRes);
	result = NImageGetHorzResolution(m_hImage, &hRes);
	result = NImageGetStride(m_hImage, &stride);

	apLog::print(0, apLog::info, "%d X %d, pixel format: 0x%X, size %d",
		width, height, pf, image_size);
	apLog::print(0, apLog::info, "hor Res: %.3f, vert Res: %.3f, stride %d", 
		hRes, vRes, stride);
}

void apSSImage::CleanImage()
{
	if(m_hImage)
	{
		NObjectFree(m_hImage);
		m_hImage = NULL;
	}
}

bool apSSImage::SaveImage_file(const char* filename)
{
	NResult result;

	if(!m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is null, failed to save");
		return false;
	}

	result = NImageSaveToFile(m_hImage, filename, NULL);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to save image(result = %d)", result);
		return false;
	}

	apLog::print(0, apLog::detail, "Image file %s saved", filename);
	return true;
}

bool apSSImage::SaveImage_data(char *pData, int *dataLen)
{
	NResult result;

	if(!m_hImage)
	{
		apLog::print(0, apLog::error, "internal image is null, failed to save");
		return false;
	}

	/*result = NImageCopyToData(m_hImage, filename, NULL);
	if(NFailed(result))
	{
		apLog::print(0, apLog::error, "failed to save image(result = %d)", result);
		return false;
	}*/

	apLog::print(0, apLog::detail, "Image data %s saved");
	return true;
}